package com.fengxing.ams.pub.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.fengxing.ams.pub.util.Collections3;
import com.google.common.collect.Lists;

public class DynamicSpecifications {

	public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz, final Collection<String> groupFields) {
		return new Specification<T>() {
			@Override
			public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				Predicate predicate = null;
				if (Collections3.isNotEmpty(filters)) {
					List<Predicate> predicates = Lists.newArrayList();
					for (SearchFilter filter : filters) {
						// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
						String[] names = StringUtils.split(filter.fieldName, ".");
						Path expression = root.get(names[0]);
						for (int i = 1; i < names.length; i++) {
							expression = expression.get(names[i]);
						}

						// logic operator
						switch (filter.operator) {
						case EQ:
							predicates.add(builder.equal(expression, filter.value1));
							break;
						case NEQ:
							predicates.add(builder.notEqual(expression, filter.value1));
							break;
						case LIKE:
							predicates.add(builder.like(expression, filter.value1.toString()));
							break;
						case GT:
							predicates.add(builder.greaterThan(expression, (Comparable) filter.value1));
							break;
						case LT:
							predicates.add(builder.lessThan(expression, (Comparable) filter.value1));
							break;
						case GTE:
							predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value1));
							break;
						case LTE:
							predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value1));
							break;
						case BETWEEN:
							predicates.add(builder.between(expression,  (Comparable) filter.value1,  (Comparable) filter.value2));
							break;
						case IN:
							In in = builder.in(expression);
							for (Object obj : filter.splits) {
								in.value(obj);
							}
							predicates.add(in);
							break;
						}
					}
					// 将所有条件用 and 联合起来
					if (!predicates.isEmpty()) {
						query.where(builder.and(predicates.toArray(new Predicate[predicates.size()])));
					}
					
				}
				if (!groupFields.isEmpty()) {
					List<Expression<?>> groupExps = new ArrayList<Expression<?>> (groupFields.size());
					for (String field : groupFields) {
						Path expression = root.get(field);
						groupExps.add(expression);
					}
					query.groupBy(groupExps);
				}
				
				return query.getRestriction();
			}
		};
	}
	
	public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) {
		return new Specification<T>() {
			@Override
			public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				if (Collections3.isNotEmpty(filters)) {
					List<Predicate> predicates = Lists.newArrayList();
					for (SearchFilter filter : filters) {
						// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
						String[] names = StringUtils.split(filter.fieldName, ".");
						Path expression = root.get(names[0]);
						for (int i = 1; i < names.length; i++) {
							expression = expression.get(names[i]);
						}

						// logic operator
						switch (filter.operator) {
						case EQ:
							predicates.add(builder.equal(expression, filter.value1));
							break;
						case NEQ:
							predicates.add(builder.notEqual(expression, filter.value1));
							break;
						case LIKE:
							predicates.add(builder.like(expression, filter.value1.toString()));
							break;
						case GT:
							predicates.add(builder.greaterThan(expression, (Comparable) filter.value1));
							break;
						case LT:
							predicates.add(builder.lessThan(expression, (Comparable) filter.value1));
							break;
						case GTE:
							predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value1));
							break;
						case LTE:
							predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value1));
							break;
						case BETWEEN:
							predicates.add(builder.between(expression,  (Comparable) filter.value1,  (Comparable) filter.value2));
							break;
						case IN:
							In in = builder.in(expression);
							for (Object obj : filter.splits) {
								in.value(obj);
							}
							predicates.add(in);
							break;
						}
					}
					// 将所有条件用 and 联合起来
					if (!predicates.isEmpty()) {
						return builder.and(predicates.toArray(new Predicate[predicates.size()]));
					}
				}
				return builder.conjunction();
			}
		};
	}
}
